<template>
    <div class="theory">
    <h3>发表评论</h3>
    <hr>
    <textarea placeholder="请输入要BB的内容（最多吐槽120字）" maxlength="120" v-model="msg"></textarea>

    <mt-button type="primary" size="large" @click="addmove">发表评论</mt-button>

    <div class="cmt-list" v-for = "(item,index) in addcommentlist" :key="index">
      <div class="cmt-item">
        <div class="cmt-title">
          第{{index+1}}楼&nbsp;&nbsp;用户：{{item.user_name}} &nbsp;&nbsp;发表时间：{{item.add_time | time}}
        </div>
        <div class="cmt-body">
         {{item.content}}
        </div>
      </div>
    </div>

    <mt-button type="danger" size="large" plain  @click="getMore">加载更多</mt-button>
  </div>
</template>
           
<script>
import {addcomment,addlist} from "@/api/index.js";
import { Toast } from "mint-ui";
export default {
    data () {
      return {
        pageindex :1,
        addcommentlist:[],
        id: this.$route.params.id,
        msg:"",
      }
    },
    created(){
       this.add()
    },
    methods:{
      add(id,pageindex){
        addcomment(this.id, this.pageindex).then( res=>{
          console.log(res) 
         this.addcommentlist = this.addcommentlist.concat(res.message);
        })
      },
      getMore(){
              this.pageIndex++;
              this.add();
      },
      addmove(){
                if (this.msg.trim().length === 0) {
                return Toast("请填写你的评论");    
                  }
              addlist(this.id,{content: this.msg.trim()}).then(res =>{
                if (res.status === 0) {
                    var cmt = {
                      user_name: "匿名用户",
                      add_time: Date.now(),
                      content: this.msg.trim()
                    };
                    this.addcommentlist.unshift(cmt);
                    this.msg = "";
                  }
              })
      }
    }
}
</script>
<style lang="scss" scoped>
.theory {
  h3 {
    font-size: 18px;
  }
  textarea {
    font-size: 14px;
    height: 85px;
    margin: 0;
  }

  .cmt-list {
    margin: 5px 0;
    .cmt-item {
      font-size: 13px;
      .cmt-title {
        line-height: 30px;
        background-color: rgb(231, 229, 229);
      }
      .cmt-body {
        line-height: 35px;
        text-indent: 2em;
      }
    }
  }
}
</style>
